<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Menu_model extends CI_Model{
		
		function __construct()
		{
			parent::__construct();
			$this->table = 'menu';
		}
		
		
		function get_data(){
			 $query = $this->db->get($this->table)->result_array();
			 $k=0;
			  foreach($query as $key=>$row) {
					if($row['pid'] == 0) {
						$result[$row['id']] = $row;
						$index[$row['id']] =& $result[$row['id']];
						$k=0;
					}else {
						//$index[$row['pid']][$row['id']] = $row;
						//$index[$row['id']] =& $index[$row['pid']][$row['id']];
						
						$index[$row['pid']]['son'][$k] = $row;
						$index[$row['id']] =& $index[$row['pid']]['son'][$k];
						$k++;
					}
				}
				return $result;
		}
		
		public function GetAll()
		{
			$this->db->select('*');
			$this->db->from($this->table);
			$this->db->where('hide','0');
			$this->db->order_by('sort,id asc');
			$query = $this->db->get();
			return $query->result_array();
		}
		
		public function GetName($url)
		{
			$this->db->select('title');
			$this->db->from($this->table);
			$this->db->where('url',$url);
			$this->db->order_by('sort,id asc');
			$query = $this->db->get()->row_array();
			return $query['title'];
		}
		
		
		public function findkey(&$a,$id){
			foreach($a as $k=>$t){
				if($t['url']==$id)return $k;
				//print_r($t);die();
				/*
				foreach($t as $k1=>$t1){
					if(in_array($id,$t1))
					return $k;
				}
				*/
				
			}
			
			return FALSE;
		}
		
		//递归生成树形数组
		public function tree($table,$pid='0') {
			$tree = array();
			foreach($table as $row){
				if($row['pid']==$pid){
					$tmp = $this->tree($table,$row['id']);
					if($tmp){
						$row['children']=$tmp;
					}else{
						$row['leaf'] = true;
					}
					$tree[]=$row;                
				}
			}
			return $tree;        
		}
		
		public function getlist($pid)
		{
			$this->db->select('*');
			$this->db->from($this->table);
			$this->db->where('pid',$pid);
			$this->db->order_by('sort,id asc');
			//$this->db->limit($limit);
			$query = $this->db->get();
			return $query->result_array();
		}
		
		function get_list()
		{
			return $query = $this->db->get($this->table)->result_array();
		}
		
		function get_tree($parent_id = null)
		{
			$this->db->where('pid',$parent_id);
			$this->db->select('*');
			$query = $this->db->get($this->table);
			$list['son'] = $query->result_array();
			
				foreach ($list['son'] as &$row) {
					if(array_key_exists('last',$row))
					{
						if(!$row['last'])
						{
						$temp = $this->get_tree($row['id']);//这里用了个递归
						$row['son'] = $temp['son'];
						}
					}
				 
				}
			
			return $list;
		}
		function showbyselect($result,$layer=1){
		
			 //用来跟踪当前菜单的级数
			$root=$this->db->select('*')->where('pid','0')->get($this->table);
			return $this->_showbyselect($root,$layer);
			
		}
		
		
		
		function _showbyselect($result,$layer){  
			$rows=$result->num_rows();
			$menu=$result->result_array();
			$select="";    
			foreach($menu as $row){    
			  $channelid=trim($row['id']);
			  $select.="<option value='$channelid'>";
			  for($i=1;$i<$layer;$i++){
				 $select.="|——";
			  }
			  $select.=$row['title'];
			  $select.="</option>";  
			  
			  
			  
			  $sql_sub = $this->db->select('*')->where('pid',$channelid)->get($this->table);
			
			  //$sql_sub="select * from ulife_menu where pid='$channelid'";
			  $result_sub=$this->db->query($sql_sub); //echo   $result_sub->num_rows() ;
									   
			  if($result_sub->num_rows()>0){    
				$layer++;      
				$select.=$this->showbyselect($result_sub,$layer);        
				$layer--;
			  }    
			}  
			return $select;
		}
		
		

		/*
        $this->db->order_by("layer字段", "asc");
        $this->db->order_by("orders字段", "asc");
        $data = $this->db->get('表');
 
        foreach($data->result_array() as $row) {
 
            if($row['group_parent_id'] == $gid) {
 
                $result[$row['gid']] = $row;
 
                $index[$row['gid']] =& $result[$row['gid']];
 
            }else {
 
                $index[$row['group_parent_id']][$row['gid']] = $row;
 
                $index[$row['gid']] =& $index[$row['group_parent_id']][$row['gid']];
 
            }
        }
 
        return $result;
		*/
		
		
	}